#include "defines.h"

#define TSTR    ((volatile uint8 *) 0xffff60)
#define TISRA   ((volatile uint8 *) 0xffff64)
#define TCR0    ((volatile uint8 *) 0xffff68)
#define GRA0    ((volatile uint16 *)0xffff6c)

void delay_us(unsigned short us)
{
    unsigned short i;
    
    *TCR0 = 0x20;
    *GRA0 = 0x0013;
    
    *TSTR |= 0x01;
    
    for (i = 0; i < us; i++) {
        while(!(*TISRA & 0x01))
            ;
        *TISRA &= 0xfe;
    }
    
    *TSTR &= 0xfe;
}

void delay_ms(unsigned short ms)
{
    unsigned short i;
    
    *TCR0 = 0x23;
    *GRA0 = 0x09c3;
    
    *TSTR |= 0x01;
    
    for (i = 0; i < ms; i++) {
        while(!(*TISRA & 0x01))
            ;
        *TISRA &= 0xfe;
    }
    
    *TSTR &= 0xfe;
}
